<?php

namespace App\Http\Controllers;

use App\Ordertask;
use App\Task;
use App\Work;
use Illuminate\Http\Request;
use App\Order;
use App\Finish;
use App\Rule;
class TaskController extends Controller
{
    public function __construct() {
        $this->middleware('auth');
    }

    public function getTasks(Request $request)
    {
        if($request->input('date') != null){
            $date=$request->input('date');
        }else{
            $date=Carbon::now();
        }

        $date=date('Y-m-d',strtotime($date) );
        $tasks=Task::where('date',$date)->get();
        return response()->json($tasks);

    }

    public function delTask($id )
    {
        $tasks=Task::find($id);
        $oTId=$tasks->orderTaskId;
        $tasks->delete();
        $orderTask=Ordertask::find($oTId);
        $orderTask->alloted=Task::where('orderTaskId',$orderTask->id)->sum('output');
        $orderTask->save();

        $allotSum=Ordertask::where('orderId',$orderTask->orderId)->sum('alloted');
        $outputSum=Ordertask::where('orderId',$orderTask->orderId)->sum('output');
        $order=Order::find($orderTask->orderId);
        $order->alloted=$allotSum.'/'.$outputSum;
        $order->save();

    }

    public function info()
    {
        $uid=\Auth::id();
        $rules=Rule::where('uid',$uid)->get();
        return view('info.info',['rules'=>$rules]);
    }

    public function getInfo()
    {

        $infos=Finish::orderBy('created_at', 'desc')->get();
        return response()->json($infos);
    }
    public function editFinish(Request $request)
    {
        $finish=$request->input('finish');
        $id=$request->input('id');
        $taskFinish=Finish::find($id);
        $taskFinish->log='('.$taskFinish->finish.')';
        $taskFinish->finish=$finish;
        $taskFinish->save();


        $task=Task::find($taskFinish->taskId);
        $task->finish =$task->finishs()->sum('finish');
        $task->save();

        $orderTask=Ordertask::find($taskFinish->orderTaskId);
        $orderTask->finished = $childOrdertaskFinish =$orderTask->tasks()->sum('finish');
        $orderTask->save();

        $order=Order::find($taskFinish->orderId);
        $order->finished=$orderfinish=$order->ordertasks()->sum('finished');
        $order->save();
        $goodId=$taskFinish->goodsId;

        $childOrder=$order->children()->get();   //获取合并订单下的子订单信息并更新
        if($childOrder){
            foreach ($childOrder as $cO){
                Order::where('id',$cO->id)->update(['finished'=>$orderfinish]);
                Ordertask::where('orderId',$cO->id)->where('goodsId',$goodId)->update(['finished'=>$childOrdertaskFinish]);
            }
        }


        $infos=Finish::orderBy('created_at', 'desc')->get();
        return response()->json($infos);
    }
    public function getDateInfo(Request $request)
    {
        $date=$request->input('date');
        $start=$date['0'];
        $end=$date['1'];
        $infos=Finish::whereBetween('date',[$start,$end])->get();
        return response()->json($infos);
    }

    public function shift(Request $request)
    {


        $outTaslId = $request->input('outTaskId');
         $workId = $request->input('inWorkId');
        $task = Task::find($outTaslId);
        $newTask['orderTaskId'] = $task->orderTaskId;
        $newTask['goodsName'] = $task->goodsName;
        $newTask['goodsId'] = $task->goodsId;
        $newTask['output'] = $task->output - $task->finish;
        $newTask['note'] = '转移的任务';
        $task->output = $task->finish;
        $task->save();
        $work = Work::find($workId);
        $newTask['date'] = $work->date;
        $newTask['workId'] = $work->id;
        $newTask['workerId'] = $work->workerId;
        Task::Create($newTask);

    }

}
